package com.feynman.pattern.singleton.register;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @ClassName containerSingleton
 * @Description 注册式单例
 * @Author Feynman
 * @date 2019/4/4 22:01
 * @Version V1.0
 **/
public class containerSingleton {

    private containerSingleton(){

    }

    private static final Map<String,Object> ioc = new ConcurrentHashMap<String,Object>();

    public static Object getBean(String calssName){
        synchronized (ioc){
            if(!ioc.containsKey(calssName)){
                Object obj = null;
                try{
                    obj = Class.forName(calssName).newInstance();
                    ioc.put(calssName,obj);
                }catch (Exception e){
                    e.printStackTrace();
                }
                return obj;
            }else{
                return ioc.get(calssName);
            }
        }
    }
}
